package com.example.bdmanagerapplication

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.bdmanagerapplication.MapUtil.launchAMap
import com.example.bdmanagerapplication.databinding.ActivityRecordsBinding
import com.google.gson.Gson
import org.jetbrains.anko.startActivity

class RecordsActivity : AppCompatActivity(),RecordsAdapter.CallBack {
    private val poiDao by lazy { AppDatabase.getDatabase(this).poiInfoDao() }
    private val mViewModel by lazy { ViewModelProvider(this)[PoiInfoViewModel::class.java] }
    private val mList = ArrayList<PoiEntity>()
    private val adapter=RecordsAdapter(mList,this)
    private lateinit var mBinding:ActivityRecordsBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding=ActivityRecordsBinding.inflate(layoutInflater)
        mBinding.mNearRV.layoutManager= LinearLayoutManager(this)
        mBinding.mNearRV.adapter=adapter
        enableEdgeToEdge()
        setContentView(mBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        mViewModel.getPoiResult.observe(this){list->
            list.forEach {
                Log.d("测试数据","${it.uid},${it.name},${it.latitude},${it.longitude}")
            }
           mList.addAll(list)
            adapter.notifyDataSetChanged()
        }

        mViewModel.getAllPoi(poiDao)
    }

    override fun addItemClick(data: PoiEntity, position: Int) {

    }

    override fun itemNearClick(data: PoiEntity, position: Int) {

        startActivity<NearActivity>("entity" to data)
    }

    override fun navigateClick(data: PoiEntity, position: Int) {
        launchAMap(this,data.latitude,data.longitude,data.name)
    }
}